//==================================================================================
//		Dual Quad Board Program
//		file name : Dual_Quad.C
//		version : 1.0
//		target device : w78e516(winbond)
//		target board : dual quad board
//		c compiler : Keil
//==================================================================================

//... include file listing

#define U8 unsigned char
#define U16 unsigned int
#define S8 signed char
#define S16 signed int

#include <_REG54.h>
#include "Define.h"
#include "Global_Variable.h"
#include "Initial_MCU.h"
#include "Initial_S_P_interface.c"
#include "uart\serial.h"
#include "uart\printf.h"
#include "uart\monitor.h"

#include "Key_Check_Led_Control.c"
#include "Table_Display_Mode.H"

#include "Function_ETC.c"
#include "Table_TW2824.h"
#include "Table_TW2835.h"
//#include "Table_OSD_Font.H"
#include "Test_tbl_osd.h"
//#include "Tbl_str.h"
#include "osd.h"                        //Added for setting osd function for tw2835 (070902)
#include "osd.c"                        //Added for setting osd function for tw2835 (070902)
#include "Osd_deco.c"
#include "Initial_TW2824.c"

#include "Box.c"
#include "Wndw_pop.c"                    //Added for creating main osd for tw2835 (070903)
#include "Box_wndw.c"                       //Added for setting BOX for TW2835 (070823)
#include "OSD_Display_String.h"
//#include "Wndw_pop.c"                    //Added for creating main osd for tw2835 (070903)
#include "Camera_Title_Display.c"
#include "Display_Mode.c"
#include "Key_Function.c"


//#include "Wndw_pop.c"                    //Added for creating main osd for tw2835 (070903)
//#include	"wndw_ds_scrn.c"              //Added for creat sub window
#include "OSD_Display_String.c"
#include "Menu_Page2_Function.c"
//#include "Menu_Page6_Function.c"
#include "Menu_Page7_Function.c"
#include "Menu_Page1_Function.c"
#include "Menu_Page0_Function.c"
//#include "uart/monitor.c"



//==================================================================================
//
//Menu OSD Display & Menu Select bar Move
//
//
//
//==================================================================================
U8 big_ch = 0xff;
U8 freeze_on_off  =	0;
U8 zoom_on_off = 0;
//U8 zoom_x_pos = 0;
//U8 zoom_y_pos = 0;

code U8 strIndextoCH[8][3] = {{5,6,7},{8,9,10},{15,16,17},{15,19,17},{10,6,7},{8,9,10},{15,16,17},{5,8,15}};


void main()
{
	U8	buff = 0;
	U16 i = 0;
	g_sys_version = 126;
		
	Init_Port();
 	Init_Resetn();
	//EA = 1;		//Global interrupt
	
	KEY_OE = 1;
	BUZZER_OUT	= 1; 	
	RELAY_OUT = 1;		

	MDATA = 0xff;
	chip_sel_LE = HIGH;
	chip_sel_LE = LOW;
	MDATA = 0x00;
	LED_LE = HIGH;
	LED_LE = LOW;
	MDATA = 0x6b;
	chip_sel_LE = HIGH;
	chip_sel_LE = LOW;

 	Init_Interrupt();
	//INTERRUPT(4, serial_int);

	serial_init();
	
	for(i = 0; i < 10; i++)
	{
		Key_Check(0);
		Key_Check(1);
		//Key_Check(2);
	}

	Printf("\r\n<R> Welcome !!!!!\r\n");

	Printf("\r\n<R> System Initializing, Please Wait......\r\n");

	Factory_Reset();

	ShowVersion();
	Printf("\r\n<R> Press ENTER to active the UART......\r\n");

	Init_Video();
	
	Init_Page0();      								       //Tw2835 initialization
	Init_Page1();
	Init_Page2();

	SetOSDNoMn();                                                           //Set overlay priority of X and Y path
	WriteAsicByte(MASTER,DVC_PG1,0x05,0x04);			//... AUTO_ENHANCE=1

	//WriteAsicByte(ALL_CHIP, DVC_PG1, 0x78, 0xff);
	RemoveOSDFnt(0, 0, 89, 287);
	RemoveBox();
	WriteAsicByte(MASTER,DVC_PG1,0xab,0x0a);			//enable color bar
	//Display_Full(0);	//function: clear memory

	buff = I2C_Data_Read(rom_p0, 0x00);		//... Date Setup "Format"	1: Y-M-D  2: M-D-Y  3: D-M-Y
	switch(buff) 
	{								
		case 1 : date_format1 = 0;
				date_format2 = 0;
				break;
		case 2 : date_format1 = 0;
				date_format2 = 1;
				break;
		case 3 : date_format1 = 1;
				date_format2 = 0;
				break;
	}
	
	title_display = I2C_Data_Read(rom_p0, 0x03)&0x01;		//... Display Setup "Title"			1: ON  0: OFF	
	date_display = I2C_Data_Read(rom_p0, 0x04)&0x01;		//... Display Setup "Date/time"			1: ON  0: OFF	
	vga_mode= (I2C_Data_Read(rom_p0, 0x06)&0xf0)>>4;
	language = I2C_Data_Read(rom_p0, 0x01)&0x01;

	//Enable channel boundary of X and Y of tw2835 (070821)
	Border_Line_Setup_Initial();   
	//Set parameter of video of tw2835 (070821)
	Camera_Setup_Initial();
	//Set the motion mask area and paramete of motion for tw2835 (070822)
	//Motion_Setup_Initial();                                                     


	Write_I2C_1Byte(rtc_sadd,0x07,0x93);					//SQW Out

	serial_tx_flag_start = ON;
	serial_tx_pass = ON;
	
	for(i =0; i <60000; i++);	//delay
	channel_path_Check = 0xff;
	display_mode = REALTIME_8CH;
	display_mode_backup = display_mode;
	key_code =REALTIME_8CH_KEY_A;
	led_on_off_flag =key_code;
	//Display_8ch_specific(2);
	Display_8ch();
	//Write_CH_OSD_str(CH_FULL, strIndextoCH);
	//Display_6ch();


	alarm_on =OFF;
	list_loss_channel_info = 0;
	list_motion_channel_info = 0;
	osd_motion_channel_info = 0;
	list_alarm_channel_info = 0;
	osd_alarm_channel_info = 0;
	WriteAsicByte(MASTER,DVC_PG1,0xab,0x00);	

	while(1) 
	{	
		if(v_sync_in == 1)
		{
			v_sync_in = 0;
			Key_Check(i%2);
			i =(i+1)%30000;
			buff = I2C_Data_Read(rom_p0, 0x02);		//... System Setup "Key Lock"		1: Lock  0: Unlock
			if(buff != 0)
			{
				if((menu_on == OFF) && (key_code != MENU_KEY))
					key_code = 0;
			}
			//Set main OSD box 
			//Menu_On_Off_Funtion();

			//Printf("\r\nkey_code = [0x%x]\r\n",key_code);
			//Printf("\r\display_mode = [0x%x]\r\n",display_mode);				
			//Printf("\r\zoom_mode_in = [0x%x]\r\n",zoom_mode_in);

			switch(key_code)
			{
				case ZOOM_KEY :
					Zoom_Function_ON_OFF();
					break;
				case UP_KEY :						
				case DOWN_KEY :	
				case LEFT_KEY : 
				case RIGHT_KEY :	
				case ENTER_KEY :	
					if(zoom_mode_in == ON)
						Zoom_Function();  
					if((display_mode == PIP1_MODE) || (display_mode == PIP2_MODE))
						Display_PIP();	
					if(display_mode == SPLITE_2)
						Display_Splite_2();
					if((display_mode == QUAD_A) || (display_mode == QUAD_B))
						Display_Quad_sel();
					if(display_mode == REALTIME_8CH)
						Display_Realtime8_sel();
					break;
				case FREEZE_KEY :
					Channel_Freeze_Function_ON_OFF();
					break;
				case AUTO_KEY :
					if((auto_sequence_on!=ON)&&(loss_channel_info_backup!=0xff))
						Auto_Sequence_Function_ON_OFF();
					break;
				case MODE_KEY :	
					Mode_Change_Function();
					break;
				case QUAD_KEY :
					Quad_Display_Function();
					break;
				case REALTIME_8CH_KEY_A:
					if(display_mode != REALTIME_8CH)
						RealTime_8Ch_Display_Function();
					break;
				case POLLING_6CH_KEY:
					//Display_6ch_specific(big_ch);
					Display_6ch_specific(big_ch);
					key_code = 0x00;
					break;
				case POLLING_8CH_KEY:
					Display_8ch_specific(big_ch);
					key_code = 0x00;
					break;
				default :
					Channel_Full_Display_Function_Initial();
			}			
                      key_code = 0;//Modified By Tiger, 21:33 2010-9-19
			if(auto_sequence_on == ON)
				Auto_Sequence_Function();   

			if(display_mode == SPLITE_2)
				Splite_Auto_Move_Function();
		}		
		/* UART reveiving check and process */
		while(RS_ready( ))	
			Monitor();
	}
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void timer_0_interrupt(void) interrupt 1
{
	U8 i,j,mask_bit;

	TR0 = 0;

	remote_counter ++;
	remote_long_counter ++;

//	win_scrn_8ch_strb_delay++;

	if((remote_long_counter > 445) && (remote_long_counter < 545))
		remote_long_key_counter ++;	
	if(remote_long_counter > 600)
	{
		remote_long_key_counter = 0;
		remote_long_key_on = OFF;
		remote_data_on = OFF;
		for(i=0;i<4;i++)
			remote_data_old[i] = 0;
	}
	
	if(remote_long_key_counter > 650)
	{
		remote_long_key_on = ON;
		remote_data_on = ON;
		remote_long_key_counter = 650;
	}

	if(remote_long_key_on == OFF)
	{
		if((remote_counter > 70) && (remote_counter < 87))
			remote_start_flag = ON;	
		else if(remote_counter > 90)
		{
			remote_counter = 90;
			remote_start_flag = OFF;
		}
		
		if(remote_start_flag == ON)
		{
			if((remote_counter > 7) && (remote_counter < 12))
			{
				mask_bit = 0x80;
				i = (remote_data_write_counter-1) / 8;
				j = (remote_data_write_counter-1) % 8;

				remote_data[i] |= (mask_bit >> j);
			}
			else if(remote_counter < 7)
			{
				mask_bit = 0x80;
				i = (remote_data_write_counter-1) / 8;
				j = (remote_data_write_counter-1) % 8;

				remote_data[i] &= ((mask_bit >> j) ^ 0xff);
			}			
		}
	}
/*
	if(b_video_fmt == PAL)
	{
		if(win_scrn_8ch_strb_delay == 3)
			strobe_ch_sw_on = ON;
		if(win_scrn_8ch_strb_delay == 420)
			strobe_loss_check_on = ON;
		if(win_scrn_8ch_strb_delay == 450)
			strobe_enable_on = ON;
	}
	else
	{
		if(win_scrn_8ch_strb_delay == 3)
			strobe_ch_sw_on = ON;
		if(win_scrn_8ch_strb_delay == 350)
			strobe_loss_check_on = ON;
		if(win_scrn_8ch_strb_delay == 380)
			strobe_enable_on = ON;
	}
*/
	TH0 = 0xfd;
	TL0 = 0xfe;

	TR0 = 1;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void x0_int() interrupt 0 /* Keyboard Interrupt */
{
    	EX0 = 0;
	
	if(remote_start_flag == ON)
		remote_data_write_counter ++;
	else
		remote_data_write_counter = 0;
	
	if(remote_data_write_counter > 32)
	{
		remote_data_on = ON;
		remote_start_flag = OFF;
		remote_data_write_counter = 0;
	}
		
	remote_counter = 0;
	remote_long_counter = 0;

	EX0 = 1;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void x1_int() interrupt 2 /*V_SYNC FROM TW28xx chip */
{
	//U8 i,hold_time_read;
	
    	EX1 = 0;
		
	v_sync_in = 1;
	v_sync_count++;
	
	motion_skip_count++;
	buzzer_sound++;
	event_bitmap_counter++;
	
	if(motion_skip_count > 60)
		motion_skip_count = 0;

	if(buzzer_sound > 40)
		buzzer_sound = 0;

	if(event_bitmap_counter > 80)
		event_bitmap_counter = 0;

	if(v_sync_count > 59)
	{
		v_sync_count = 0;
		
		one_sec++;
		if(one_sec > 250)
			one_sec = 0;
              /*    //Modified By Tiger, 21:33 2010-9-19
		for(i =0; i <33; i++)
		{
			hold_time_read = Read_I2C_1Byte(rtc_sadd, i+0x09);			//... Hold Time  Counter Read
			hold_time_read++;
			if(hold_time_read > 99)
				hold_time_read = 0;
			Write_I2C_1Byte(rtc_sadd,i+0x09,hold_time_read);			//... Hold Time Counter Write
		}
              */		
	}
	EX1 = 1;
}
